/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.xmi.converter;

import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ro.pub.cs.se.moondance.MoondanceData;
import ro.pub.cs.se.moondance.model.UMLClassDiagram;
import ro.pub.cs.se.moondance.model.UMLDiagram;
import ro.pub.cs.se.moondance.model.UMLEntity;
import ro.pub.cs.se.moondance.model.UMLClass;
import ro.pub.cs.se.moondance.model.UMLInterface;
import ro.pub.cs.se.moondance.model.UMLPackage;

public class XMIClassDiagram implements IXMIUMLDiagram {

	private void addPacket(Element root, Document document, UMLPackage myPackage,
			boolean addConnections) {
		String newID = MoondanceData.newRandomString();
		myPackage.setXMIid(newID);
		UMLPackageXMI xmiPackage = new UMLPackageXMI(myPackage.getName(), newID);
		xmiPackage.serialize(root, document, myPackage, addConnections);
	}

	private void addClass(Element root, Document document, UMLClass myClass,
			boolean addConnections) {
		String newID = null;
		
		if (!addConnections) {
			newID =  MoondanceData.newRandomString();
			myClass.setXMIid(newID);
		} else
			newID = myClass.getXMIid();
		
		UMLClassXMI xmiClass = new UMLClassXMI(myClass.getName(), newID);
		xmiClass.serialize(root, document, myClass, addConnections);
	}

	private void addInterface(Element root, Document document,
			UMLInterface myInterface, boolean addConnections) {
		String newID = null;

		if (!addConnections) {
			newID =  MoondanceData.newRandomString();
			myInterface.setXMIid(newID);
		} else
			newID = myInterface.getXMIid();
		
		UMLInterfaceXMI xmiInterface = new UMLInterfaceXMI(myInterface
				.getName(), newID);
		xmiInterface.serialize(root, document, myInterface, addConnections);
	}

	@Override
	public void add(Element rootElement, Document document, UMLDiagram diagram) {
		UMLClassDiagram classDiagram = (UMLClassDiagram) diagram;
		List<UMLEntity> blocksList = classDiagram.getChildren();

		// Add external classes and interfaces
		for (Iterator<UMLEntity> it = blocksList.iterator(); it.hasNext();) {
			UMLEntity entity = it.next();

			if (entity instanceof UMLClass)
				addClass(rootElement, document, (UMLClass) entity, false);
			else if (entity instanceof UMLInterface)
				addInterface(rootElement, document, (UMLInterface) entity, false);
			else if (entity instanceof UMLPackage)
				addPacket(rootElement, document, (UMLPackage) entity, false);
		}
		
		// Add packages and connections
		for (Iterator<UMLEntity> it = blocksList.iterator(); it.hasNext();) {
			UMLEntity entity = it.next();
			
			if (entity instanceof UMLClass)
				addClass(rootElement, document, (UMLClass) entity, true);
			else if (entity instanceof UMLInterface)
				addInterface(rootElement, document, (UMLInterface) entity, true);
			else if (entity instanceof UMLPackage)
				addPacket(rootElement, document, (UMLPackage) entity, true);
		}
	}
}
